<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="hSubmit">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permissions'
import { getRoleDetail, assignPerm } from '@/api/setting'

export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 存储权限数据
    }
  },
  created() {
    // 获取权限列表
    this.loadPermissionList()
  },
  methods: {
    // 获取权限列表
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res.data)

      const { data } = await getRoleDetail(this.id)
      // console.log(data)
      this.$refs.refTree.setCheckedKeys(data.permIds)
    },
    // 点击确认
    async hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      await assignPerm({ id: this.id, permIds })
      this.$message.success('分配权限成功')
      this.$emit('close')
    }
  }
}
</script>

<style></style>
